package com.qdsgjsfk.vision.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.Toast;

import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.databinding.ActivityDeviceListBinding;
import com.qdsgjsfk.vision.eventbus.BlueTooth;
import com.qdsgjsfk.vision.util.ToastUtil;
import com.rest.response.DeviceResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import jz.joyoung.robot.util.LogUtil;

import com.qdsgjsfk.vision.adapter.DeviceListAdapter;
import com.qdsgjsfk.vision.base.BaseAppCompatActivity;
import com.qdsgjsfk.vision.eventbus.SocketInput;
import com.qdsgjsfk.vision.model.Msg;
import com.qdsgjsfk.vision.util.UtilString;
import com.qdsgjsfk.vision.viewmodel.DeviceViewModel;

import util.PreferencesHelper;

/**
 * 视力表或者验光仪型号页面
 * Created by HP on 2019/10/30.
 */

public class DeviceListActivity extends BaseAppCompatActivity {

    private DeviceListAdapter deviceListAdapter;
    private ActivityDeviceListBinding binding;
    private DeviceViewModel deviceViewModel;
    private List<DeviceResponse.Device> list = new ArrayList<>();
    public static int REQUEST_CODE_SCAN_IP = 4;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        long type = Long.parseLong(getIntent().getStringExtra("type"));
        //设备名称
        String name = getIntent().getStringExtra("name");

        binding = DataBindingUtil.setContentView(this, R.layout.activity_device_list);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        deviceListAdapter = new DeviceListAdapter(list, this, type);
        binding.recyclerView.setAdapter(deviceListAdapter);
        deviceViewModel = ViewModelProviders.of(this).get(DeviceViewModel.class);
//        binding.setVariable(BR.device,deviceViewModel);
        //设备型号选择
        deviceViewModel.getDeviceList(type, this);
        deviceViewModel.getmItems().observe(this, devices -> {
            list.clear();
            assert devices != null;
            list.addAll(devices);
            deviceListAdapter.notifyDataSetChanged();
        });
        binding.setHandlers(new EventHandlers());
        binding.setTitle("请选择设备型号");
//        if (type == 13) {
//            binding.setTitle( "请选择验光仪型号" );
//        } else {
//            binding.setTitle( "请选择视力表型号" );
//        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(BlueTooth event) {
        LogUtil.e("onMessageEvent");
        if (event.type == 100) {
            ToastUtil.showToastCenter(getApplicationContext(), event.output);
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 扫描二维码/条码回传
        if (requestCode == REQUEST_CODE_SCAN_IP && resultCode == RESULT_OK) {
            if (data != null) {
            }
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
